package org.core.tliaswebmanagement.controller;

import lombok.extern.slf4j.Slf4j;
import org.core.tliaswebmanagement.pojo.*;
import org.core.tliaswebmanagement.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/*
*使用@Scope()注解限定java bean的作用域
* 有五种作用域：singleton(单例，容器内唯一，默认)，prototype(多例，使用时创建新的)，request(请求)，session(会话)，application(应用)
* singleton默认项目启动时创建(饿汉)，加上@Lazy即可使用时再创建(懒汉)
* prototype适用于有状态的bean,即需要存储信息的bean
* bean的线程安全却决于状态及作用域,故单例无状态线程安全,单例有状态线程不安全
*/

@Slf4j
@RestController
@RequestMapping("/clazzs")
public class ClazzController {

    //如果该service被切入点捕获，则注入的是代理对象
    @Autowired
    private ClazzService clazzService;

    @GetMapping("/list")
    public Result list() {
        log.info("查询所有班主任");
        List<Clazz> clazzList = clazzService.findAll();
        return Result.success(clazzList);
    }

    @GetMapping
    public Result page(ClazzQueryParam param) {
        log.info("分页查询班级");
        PageResult<Clazz> pageResult = clazzService.page(param);
        return Result.success(pageResult);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("根据id删除班级信息：{}", id);
        clazzService.deleteById(id);
        return Result.success();
    }

    @PostMapping
    public Result add(@RequestBody Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        log.info("保存班级信息：{}", clazz);
        clazzService.add(clazz);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result get(@PathVariable Integer id) {
        log.info("根据id查询班级信息：{}", id);
        Clazz clazz = clazzService.getInfo(id);
        return Result.success(clazz);
    }

    @PutMapping
    public Result update(@RequestBody Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        log.info("更新班级信息：{}", clazz);
        clazzService.update(clazz);
        return Result.success();
    }
}
